package com.dhecp.project.common.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTableToEntity;
import com.dhecp.framework.web.service.BaseInfoSimpleService;
import com.dhecp.project.base.domain.BaseInfo;
import com.dhecp.project.common.service.ButtonStateService;
import com.dhecp.project.system.domain.SysDictionaryForPageField;
import com.dhecp.project.system.domain.SysFieldForConditions;
import com.dhecp.project.system.service.SysDictionaryForMobileFieldService;

/**
 * 精简基础数据 控制层
 * @author DH-lwl
 * @version 2020年8月5日
 */
@Controller
@RequestMapping("/base/baseinfosimple")
public class BaseInfoSimpleController extends BaseController{

	@Autowired
    private BaseInfoSimpleService baseInfoSimpleService;
	
	@Autowired
	private ButtonStateService buttonStateService;
	
	@Autowired
    private SysDictionaryForMobileFieldService sysDictionaryForMobileFieldService;
	
	/**
	 * 根据字典配置的类型返回对应下拉框待选数据
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@RequestMapping("/dropdowndatalist")
    @ResponseBody
    public TableDataInfo dropdownDataList(@RequestParam Map<String, Object> params) throws Exception{
        List<BaseInfo> list = new ArrayList<BaseInfo>();
        try {
        	list = baseInfoSimpleService.getDropdownData(params.get("fieldSearchType").toString(),params.get("fieldFrontSave").toString(),params.get("fieldFrontShow").toString());
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }
	
	/**
	 * 获取功能页面中字段及各属性(以List形式返回)
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@RequestMapping("/fieldlist")
    @ResponseBody
    public TableDataInfo fieldAttributesList(@RequestParam Map<String, Object> params) throws Exception{
		Object entity = params.get("moduleTable").toString().toLowerCase().startsWith("tb_baseinfo")?new BaseInfo():ModuleTableToEntity.queryEntityByModuleTable(params.get("moduleTable").toString()).get("entity");
		params.put("entity", entity);
        List<SysDictionaryForPageField> list = new ArrayList<SysDictionaryForPageField>(); 
        List<Map<String, Object>> fieldsList = new ArrayList<Map<String, Object>>();
        Map<String,Object> returnMap = new HashMap<String, Object>();
        try {
        	list = sysDictionaryForMobileFieldService.queryFieldAttributesList(params);
            for (SysDictionaryForPageField field : list) {
            	returnMap = TypeUtils.entityToMap(field);
            	List<BaseInfo> dropDownList = new ArrayList<BaseInfo>();
            	
            	if (field.getFieldFrontType().toLowerCase().startsWith("t_dropdown")){
            		dropDownList=baseInfoSimpleService.getDropdownData(field.getFieldSearchType(),field.getFieldFrontSave(),field.getFieldFrontShow());
            		returnMap.put("dropDownList", dropDownList);
            	}
            	fieldsList.add(returnMap);
    		}
        } catch (Exception e) {
            e.printStackTrace(); 
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(fieldsList);
    }
	
	/**
	 * 查询页面中的查询条件字段，（固定及扩展条件）
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@RequestMapping("/fieldfrontlist")
    @ResponseBody
    public TableDataInfo fieldFrontList(@RequestParam Map<String, Object> params) throws Exception{
        List<SysFieldForConditions> list = new ArrayList<SysFieldForConditions>();
        List<Map<String, Object>> fieldsList = new ArrayList<Map<String, Object>>();
        Map<String,Object> returnMap = new HashMap<String, Object>(); 
        try {
        	list = baseInfoSimpleService.getFieldFrontConditions(params.get("moduleTable").toString(), params.get("moduleTable").toString(), "page", "mobile");
        	for(SysFieldForConditions field : list){
        		returnMap = TypeUtils.entityToMap(field);
            	List<BaseInfo> dropDownList = new ArrayList<BaseInfo>();
            	
            	if (field.getFieldFrontType().toLowerCase().startsWith("t_dropdown")){
            		
            		dropDownList=baseInfoSimpleService.getDropdownData(field.getFieldSearchType(),"vid","vname");
            		returnMap.put("dropDownList", dropDownList);
            	}
            	fieldsList.add(returnMap);
        	}
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(fieldsList);
    }
	
	/**
     * 查询按钮权限
     * @param moduleTable 必要参数：功能唯一标识
     * @return Map - buttonState
     * @author DH-Xxj
     */
	@RequestMapping("/buttonstatelist")
    @ResponseBody
    public Map<String, Object> checkButtonState(@RequestParam Map<String, Object> params){
    	Map<String, Object> buttonState = new HashMap<String, Object>();	//按钮状态：（可用：enable，禁用：disable，隐藏：hidden）
    	Map<String, Object> buttonMap = new HashMap<String, Object>();
    	if (params.get("moduleTableForButton") == null || params.get("moduleTableForButton") == ""){
    		if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
        		buttonMap.put("moduleTable", "moduleTable");
    		}else{
        		buttonMap.put("moduleTable", params.get("moduleTable").toString());
    		}
    	}else{
    		buttonMap.put("moduleTable", params.get("moduleTableForButton"));
    	}
    	buttonState = buttonStateService.getButtonStateObjectForUser(buttonMap);
    	return buttonState;
    }
}
